import assert = require('assert');
import {sdf} from "../expressExt/public/sdf";
import {Meta} from "../expressExt/public/Meta";
import {FileUpload} from "../expressExt/public/FileUpload";
import {HttpClient, HttpCookie, RespContent} from "../expressExt/HttpClient";
require("../expressExt/FuncExt")
require("../expressExt/ES6Ext")
class testP {
    a = 2;
}


class testSub {
    c = 1;
}


describe('sdf Test', function () {

    it("http cookie", () => {
        var hc = new HttpClient("https://www.baidu.com/");
        hc.resp = {
            headers: {
                "set-cookie": ["BDSVRTM=127; path=/",
                    "H_PS_PSSID=1441_21080_22917_20718; path=/; domain=.baidu.com",
                    "__bsi=9759887671903292193_00_783_N_N_129_0303_C02F_N_N_Y_0; expires=Tue, 30-May-17 04:36:56 GMT; domain=www.baidu.com; path=/",
                    "BD_HOME=1; path=/",
                ]
            }
        } as any
        let co = new HttpCookie();
        co.setCookies(hc.getHostName(), hc.resp!.headers);
        assert.equal(JSON.stringify(co.cookies), `{"www.baidu.com":{"BDSVRTM":"127","H_PS_PSSID":"1441_21080_22917_20718","__bsi":"9759887671903292193_00_783_N_N_129_0303_C02F_N_N_Y_0","BD_HOME":"1"},".baidu.com":{"H_PS_PSSID":"1441_21080_22917_20718"}}`);

        hc = new HttpClient("https://www.baidu.com/");
        hc.header.setCookie(co.getCookisStr(hc.getHostName()))

        assert.equal(hc.getHeaders()["Cookie"], `BDSVRTM=127; H_PS_PSSID=1441_21080_22917_20718; __bsi=9759887671903292193_00_783_N_N_129_0303_C02F_N_N_Y_0; BD_HOME=1`);

    });


    it("loadBalance", () => {
        let allCount = 30000;
        let serverCount = 10;

        let arr = Array<Array<any>>();
        for (let i = 0; i < serverCount; i++) {
            arr.push([])
        }


        for (let i = 0; i < allCount; i++) {
            let code = sdf.randomInt(100000)
            let index = sdf.loadBalance(code, serverCount);
            arr[index].push(code);
        }

        arr.push([]);

        let count = 0;
        let moveCount = 0;

        for (let y = 0; y < serverCount; y++) {
            for (let i = 0; i < arr[y].length; i++) {
                let code = arr[y][i]
                let index = sdf.loadBalance(code, serverCount + 1);
                if (index == serverCount) {
                    count++;
                    moveCount++;
                }

                if (index == y)
                    count++;
            }
        }
        assert.equal(count, allCount)

    })

    it("matchSuffiix", () => {
        let a = 10;
        let b = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
        let c = 0;
        a.loop(i => {
            assert.equal(i, b[i]);
            c++;
        })


        assert.equal(c, a);
        let res = a.loopMap(i => i);
        assert.equal(JSON.stringify(b), JSON.stringify(res));

        res = Number(10).loopMap(i => i);
        assert.equal(JSON.stringify(b), JSON.stringify(res));
        assert.equal(sdf.getFileName("dddd/dsds/233/jj.abc"), "jj.abc");
        assert.equal(sdf.getFileName("ddddjj.abc"), "");
        assert.equal(sdf.getFileName("/ddddjj.abc"), "ddddjj.abc");
        assert.equal(FileUpload.matchSuffix("aaa.jpg", FileUpload.imageArr), true)
        assert.equal(FileUpload.imageType, ".jpeg,.jpg,.bmp,.png")
    })


    it("mapString", () => {
        let a = [1, 2, 3, 4];
        assert.equal(a.mapString(r => r), "1234");
    })


    it("binarySearch", () => {
        let a = [1, 2, 3, 4];
        assert.equal(a.binarySearch(r => 0 - r), -1);
        assert.equal(a.binarySearch(r => 1 - r), 0);
        assert.equal(a.binarySearch(r => 2 - r), 1);
        assert.equal(a.binarySearch(r => 3 - r), 2);
        assert.equal(a.binarySearch(r => 4 - r), 3);
        assert.equal(a.binarySearch(r => 5 - r), -1);

        a = [1, 2, 3];
        assert.equal(a.binarySearch(r => 0 - r), -1);
        assert.equal(a.binarySearch(r => 1 - r), 0);
        assert.equal(a.binarySearch(r => 2 - r), 1);
        assert.equal(a.binarySearch(r => 3 - r), 2);
        assert.equal(a.binarySearch(r => 4 - r), -1);
        assert.equal(a.binarySearch(r => 5 - r), -1);

        a = [1, 2];
        assert.equal(a.binarySearch(r => 0 - r), -1);
        assert.equal(a.binarySearch(r => 1 - r), 0);
        assert.equal(a.binarySearch(r => 2 - r), 1);
        assert.equal(a.binarySearch(r => 3 - r), -1);
        assert.equal(a.binarySearch(r => 4 - r), -1);
        assert.equal(a.binarySearch(r => 5 - r), -1);

        a = [1];
        assert.equal(a.binarySearch(r => 0 - r), -1);
        assert.equal(a.binarySearch(r => 1 - r), 0);
        assert.equal(a.binarySearch(r => 2 - r), -1);
        assert.equal(a.binarySearch(r => 3 - r), -1);
        assert.equal(a.binarySearch(r => 4 - r), -1);
        assert.equal(a.binarySearch(r => 5 - r), -1);

        a = [];
        assert.equal(a.binarySearch(r => 0 - r), -1);
        assert.equal(a.binarySearch(r => 1 - r), -1);
        assert.equal(a.binarySearch(r => 2 - r), -1);
        assert.equal(a.binarySearch(r => 3 - r), -1);
        assert.equal(a.binarySearch(r => 4 - r), -1);
        assert.equal(a.binarySearch(r => 5 - r), -1);

        let b = [{a: "1"}, {a: "2"}, {a: "3"}];

        assert.equal(b.binarySearch(r => "1".localeCompare(r.a)), 0);
        assert.equal(b.binarySearch(r => "2".localeCompare(r.a)), 1);
        assert.equal(b.binarySearch(r => "3".localeCompare(r.a)), 2);
        assert.equal(b.binarySearch(r => "aa".localeCompare(r.a)), -1);

    });

    it("toap", () => {
        let a = [1, 2, 3];
        let b1 = sdf.arrToMap(a, ot => ot);
        assert.equal(JSON.stringify(b1), `{"1":1,"2":2,"3":3}`);
        let b2 = sdf.mapToArr(b1);
        assert.equal(JSON.stringify(b2), `[1,2,3]`);
    });

    it("test", () => {
        assert.equal(sdf.isInt("123"), true);
        assert.equal(sdf.isInt("01230"), true);
        assert.equal(sdf.isInt("-123"), false);
        assert.equal(sdf.isInt(""), false);
        assert.equal(sdf.isInt("212.22"), false);
        assert.equal(sdf.isInt("asas2323"), false);
        assert.equal(sdf.isInt("3232sdsd"), false);
    });

    it("xss test", () => {
        let sss = `<html>

<head>
  <script type="text/javascript" src="loadxmldoc.js">
</script>
</head>

<body>
<iframe src="1223"></iframe>
<frameset cols="25%,50%,25%">
  <frame src="frame_a.htm" />
  <frame src="frame_b.htm" />
  <frame src="frame_c.htm" />
</frameset>

  <script type="text/javascript">
    xmlDoc=loadXMLDoc("books.xml");
    document.write("xmlDoc is loaded, ready for use");
  </script>

</body>

</html>`
        let res = sdf.xssFilter(sss);

        assert.equal(res, `<html>

<head>
  &lt;script type="text/javascript" src="loadxmldoc.js">
&lt;/script&gt;
</head>

<body>
&lt;iframe src="1223">&lt;/iframe&gt;
&lt;frameset cols="25%,50%,25%">
  &lt;frame src="frame_a.htm" />
  &lt;frame src="frame_b.htm" />
  &lt;frame src="frame_c.htm" />
&lt;/frameset&gt;

  &lt;script type="text/javascript">
    xmlDoc=loadXMLDoc("books.xml");
    document.write("xmlDoc is loaded, ready for use");
  &lt;/script&gt;

</body>

</html>`);

    });


    it("meta parent", () => {
        let e = new testSub();


        Meta.setParent(e.constructor, testP)


        let ps = Meta.getParent(e.constructor);

        assert.equal(ps.length, 1)
        assert.equal(ps[0], testP)

        Meta.setParent(e.constructor, testSub)
        ps = Meta.getParent(e.constructor);
        assert.equal(ps.length, 3)
        assert.equal(ps[0], testP)
        assert.equal(ps[2], testSub)


    })

    it("mapPromie", async () => {

        let a = [1, 2, 3, 4, 5];
        let b = await a.mapPromise(async (it) => it);


        assert.equal(JSON.stringify(a), JSON.stringify(b))
    })


    it('leftJoin', function () {
        let m = [
            {
                id: 1,
                name: "abc",
            },
            {
                id: 1,
                name: "abc22222222",
            },
            {
                id: 2,
                val: 0,
                name: "aaa",
            },
            {
                id: 3,
                name: "bbb",
            },
        ]

        let r = [
            {
                id: 2,
                val: 1,
                aa: "2222",
            },
            {
                id: 3,
                aa: "33333",
            },
        ];
        let res = [
            {
                id: 1,
                name: "abc",
            },
            {
                id: 1,
                name: "abc22222222",
            },
            {
                id: 2,
                val: 0,
                name: "aaa",
                aa: "2222",
            },
            {
                id: 3,
                name: "bbb",
                aa: "33333",
            },
        ];

        let join = sdf.leftJoin(m, r, l => l.id, r => r.id);
        assert.equal(JSON.stringify(res), JSON.stringify(join));

    });


    it("leftJoin override", () => {
        let m = [
            {
                id: 1,
                name: "abc",
            },
            {
                id: 1,
                name: "abc22222222",
            },
            {
                id: 2,
                val: 0,
                name: "aaa",
            },
            {
                id: 3,
                name: "bbb",
            },
        ]

        let r = [
            {
                id: 2,
                val: 1,
                aa: "2222",
            },
            {
                id: 3,
                aa: "33333",
            },
        ];
        let res = [
            {
                id: 1,
                name: "abc",
            },
            {
                id: 1,
                name: "abc22222222",
            },
            {
                id: 2,
                val: 1,
                name: "aaa",
                aa: "2222",
            },
            {
                id: 3,
                name: "bbb",
                aa: "33333",
            },
        ];

        let join = sdf.leftJoin(m, r, l => l.id, r => r.id, true);
        assert.equal(JSON.stringify(res), JSON.stringify(join));
    });


    it("joinObj2", () => {
        let l = {
            a: 1,
            b: 2,
            c: "3",
        }

        let r = {
            a: 22,
            d: 4,
            e: 5,
            f: "6",
        }

        let res = {
            a: 22,
            b: 2,
            c: "3",
            d: 4,
            e: 5,
            f: "6",
        };

        let r3 = {
            g: 3,
            dd: 3,
        }
        let res2 = {
            a: 22,
            b: 2,
            c: "3",
            d: 4,
            e: 5,
            f: "6",
            g: 3,
            dd: 3,
        };

        assert.equal(JSON.stringify(sdf.joinObj(l, r)), JSON.stringify(res))
        assert.equal(JSON.stringify(sdf.joinObjFast({}, l, r, r3)), JSON.stringify(res2))
    });
});